<?php  

function get_realisasi($tanggal , $karyawan_id, $waktu_kerja_id, $kelompok_shift_id, $kelompok ){
	$datas=array();
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	$ch = "SELECT * FROM a_realisasi 
		WHERE karyawan_id = {$karyawan_id} 
			AND field_tgl='tgl_{$dd}' 
			ORDER BY realisasi_id 
			DESC LIMIT 1";
	$chr = my_query($ch);
	$row = my_fetch_array($chr);

	$status_absen_id = isset($row['status_absen_id']) ? $row['status_absen_id'] : 0;  
	$statuss=my_get_data_by_id('a_status_absen','status_absen_id',$status_absen_id);
	$datas['r_status']=$statuss['initial_absen'];
	
	$datas['r_jadwal_kerja']=' ';

	$jam_datang=$row['jam_datang'] ;
	if($jam_datang=='00:00:00'){
		$jam_datang='';
	}	
	$jam_datang=(trim($jam_datang)<>'') ? date('H:i',strtotime($jam_datang)) :'';
	$jam_pulang=$row['jam_pulang'] ;
	if($jam_pulang=='00:00:00'){
		$jam_pulang='';
	}	
	$jam_pulang=(trim($jam_pulang)<>'') ? date('H:i',strtotime($jam_pulang)) :'';
	$datas['r_time']=$jam_datang.'-'.$jam_pulang ;

	$sc_jadwal_kerja_kelompok_id=(int) $row['sc_jadwal_kerja_kelompok_id'];
	$sc_status_absen_id=(int) $row['sc_status_absen_id'];
	$statuss = my_get_data_by_id('a_status_absen','status_absen_id',$sc_status_absen_id);
	$datas['s_status']=$statuss['initial_absen'] ;

	if($sc_status_absen_id>0){
		$datas['s_jadwal_kerja']=' ' ;
		$datas['s_time']=' '; 
	}else{
		if($sc_jadwal_kerja_kelompok_id>0){ 
			$query = "SELECT b.label,a.kelompok,a.waktu_kerja_id FROM a_jadwal_kerja_kelompok a 
				INNER JOIN a_waktu_kerja b ON a.waktu_kerja_id=b.waktu_kerja_id 
				WHERE a.jadwal_kerja_kelompok_id=".$sc_jadwal_kerja_kelompok_id." 
				ORDER BY a.jadwal_kerja_kelompok_id ";
			$jadwal_kerjas = my_fetch_array(my_query($query));	
			$waktu_kerja_id=$jadwal_kerjas['waktu_kerja_id'] ;
			$kelompok=$jadwal_kerjas['kelompok'] ;
			$datas['s_jadwal_kerja']=$jadwal_kerjas['label'].'/'.$kelompok ;
		}else{ //ambil schedule
			$jadwal_kerjas = my_get_data_by_id('a_waktu_kerja','waktu_kerja_id',$waktu_kerja_id);
			if($waktu_kerja_id>3){
				$kelompok=get_kelompok_shift_jadwal($tanggal, $kelompok_shift_id );		
				if($sc_status_absen_id==0){
					$datas['s_status']=$kelompok ;
				}
			}else{
				if(get_hari_libur($tanggal)){	
					$datas['s_status']= 'X';
				}else{
					$datas['s_status']= 'N';
				}
			}
			$datas['s_jadwal_kerja']=' ' ;
		}
		if($datas['s_status']== 'X'){
			$datas['s_time']='' ;		
		}else{
			$hari_id=get_hari_id_by_date($tanggal);
			$query = "SELECT jam_masuk,jam_pulang FROM a_jadwal_kerja 
					WHERE waktu_kerja_id =".$waktu_kerja_id."  
					AND kelompok='".$kelompok."' 
					AND hari_id={$hari_id}";
			$jams = my_fetch_array(my_query($query));	
			$jam_datang=$jams['jam_masuk'] ;
			$jam_datang=(trim($jam_datang)<>'') ? date('H:i',strtotime($jam_datang)) :'';
			$jam_pulang=$jams['jam_pulang'] ;
			$jam_pulang=(trim($jam_pulang)<>'') ? date('H:i',strtotime($jam_pulang)) :'';
			$datas['s_time']=$jam_datang.'-'.$jam_pulang ;		
		}
	}	
	return $datas;	
}

function get_hari_id_by_date($date){
	$hari_id=date("N",strtotime($date) ); //Monday to Sunday
	//konversi ke Sunday to Saturday
	if($hari_id==7){
		$hari_id=1;
	}else{
		$hari_id++;
	}		
	return $hari_id;
}

function get_kelompok_shift_jadwal($tanggal , $kelompok_shift_id ){
	$periode_id=get_periode_absen();
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	$query = "SELECT tgl_{$dd} FROM a_kalendar_shift 
		WHERE periode_id = {$periode_id} 
		AND kelompok_shift_id={$kelompok_shift_id}";
	$result = my_query($query);
	$re = my_fetch_array($result);
	return $re['tgl_'.$dd];
}

function get_hari_libur($date){
	if(is_sabtu_minggu($date)){
		return true;
	}
	$query="SELECT hari_libur_id FROM a_hari_libur
		WHERE tanggal = '{$date}'";
	if (my_num_rows( my_query($query)) >0){	
		return true;
	}	
	return false;
}

function detail_kelompok_shift( $karyawan_id  ){

	$karyawan = loaddata_karyawan($karyawan_id , false);
	$periode_id =get_periode_absen();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	$kelompok_shift_id=$karyawan['kelompok_shift_id'] ;
	if($bulan =='1' ){
		$startdate = ($tahun -1).'-12-11';
		$middledata = ($tahun -1).'-12-25';
		$middledata2 = ($tahun -1).'-12-26';
		$enddate = $tahun.'-'.sprintf( '%02d',$bulan) .'-10'; //hasilnya 10
	}else{
		$startdate = $tahun.'-'.sprintf( '%02d',$bulan-1) .'-11';
		$middledata =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-25';
		$middledata2 =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-26';
		$enddate =$tahun.'-'.sprintf( '%02d',$bulan ) .'-10'; //hasilnya 10
	}
	$tanggals = list_kalender($startdate , $enddate  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'DT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:25px'),  
		'S. Status'=>array('style'=>'text-align:left;border-bottom:2px solid;width:100px'),   
		'S. Jadwal'=>array('style'=>'text-align:left;border-bottom:2px solid;width:100px'),   
		'S. Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:100px'),   
		'R. Status'=>array('style'=>'text-align:left;border-bottom:2px solid;width:100px'),   
		'R. Jadwal'=>array('style'=>'text-align:left;border-bottom:2px solid;width:100px'),   
		'R Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:100px'),   
	);
	$row= array();
	foreach($tanggals as $tanggal){
		$datas=get_realisasi($tanggal , 
			$karyawan_id, 
			(int) $karyawan['waktu_kerja_id'],
			$karyawan['kelompok_shift_id'],
			$karyawan['kelompok'] ) ;	
			if(get_hari_libur($tanggal)){	
				$row[] = array(
					'tanggal'	=> '<font color="red">'.date('d',strtotime($tanggal)).'*</font>' ,
					's_status' 	=>$datas['s_status'],
					's_jadwal'	=>$datas['s_jadwal_kerja'], 
					's_time' 	=>$datas['s_time'], 
					'r_status' 	=>$datas['r_status'], 
					'r_jadwal'	=>$datas['r_jadwal_kerja'], 
					'r_time'	=>$datas['r_time'], 
				);
			}else{	
				$row[] = array(
					'tanggal'	=> date('d',strtotime($tanggal)), 
					's_status' 	=>$datas['s_status'],
					's_jadwal'	=>$datas['s_jadwal_kerja'], 
					's_time' 	=>$datas['s_time'], 
					'r_status' 	=>$datas['r_status'], 
					'r_jadwal'	=>$datas['r_jadwal_kerja'], 
					'r_time'	=>$datas['r_time'], 
				);
			}	
	}
	$datas = table_cetak_rows($row);
	$table1 = table_cetak_builder($header , $datas ,  2 , false); 
	return $table1;
}
?>
<style type="text/css">
<!--

	table.page_header {width: 100%; border: none; background-color: #CDCDCD; border-bottom: solid 1mm #000; padding: 2mm }
	table.page_footer {width: 100%; border: none;  border-top: solid 1mm #000; padding: 2mm}
div.zone
{
	border: solid 2mm #66AACC;
	border-radius: 3mm;
	padding: 1mm;
	background-color: #FFEEEE;
	color: #440000;
}
div.zone_over
{
	width: 30mm;
	height: 35mm;
	overflow: hidden;
}

-->
</style> 	 
<page style="font-size: 10pt">
	<?php
	$karyawan = loaddata_karyawan($_GET['karyawan_id']);
	
	$periode_id =get_periode_absen();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ; 
	?>
	<div style="width: 100%; text-align:center; padding:2px; font-size: 13pt ;">Detail realisasi kehadiran<br> </div>
	<br>
	<table style="width:100%;">
	<tr>
		<td style="font-size:12px;width:35%;height:20px;">NAMA KARYAWAN/ NIK</td>
		<td style="width:65%"><?php echo $karyawan['nama_gelar'].'/ '. $karyawan['nik'];?></td>
	</tr>
	<tr>
		<td style="font-size:12px;width:35%;height:20px;">Periode Absen</td>
		<td style="width:65%"><?php echo sprintf( '%02d',$bulan ).'-'.$tahun;?></td>
	</tr>
	</table><br>
	<?php echo detail_kelompok_shift( $_GET['karyawan_id']); ?> 
</page> 